/*
 * 
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding GB2312
 */
package net.comm.group.service.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.Vector;

import net.comm.common.Fileload;
import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;
import net.comm.oper.service.impl.OperLogServiceImpl;
import net.comm.oper.web.form.OperLogForm;
import net.comm.param.service.impl.SequenceMgr;
import net.comm.common.common;


import net.comm.file.service.impl.FilesServiceImpl;
import net.comm.file.web.form.FilesForm;
import net.comm.group.bean.BaseGPicture;
import net.comm.group.db.DbGForumService;
import net.comm.group.db.DbGPictureService;
import net.comm.group.db.GForumServiceSqlMysql;
import net.comm.group.db.GPictureServiceSqlMysql;
import net.comm.group.web.form.GAlbumForm;
import net.comm.group.web.form.GForumForm;
import net.comm.group.web.form.GPictureForm;
import net.comm.group.web.form.GdianpingForm;
import net.comm.page.PageCtr;

import net.comm.tags.JstlFun;
import net.comm.util.DatetimeUtil;
import net.comm.util.DbcpPool;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.apache.struts.upload.FormFile;
import org.sakaiproject.db.impl.BasicSqlService;


/**
 *
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class GPictureServiceImpl {

	static Logger logger = Logger.getLogger(GPictureServiceImpl.class.getName());

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List list(GPictureForm theForm,  ReturnMessage msg) 
	{
		List list = null;

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			GPictureServiceSqlMysql servSql = new GPictureServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getCountSql1();

			Object[] fields2 = new Object[1];
			
			
			fields2[0] = theForm.getQryGroupID();

			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(bSqlServ.dbCount(cn, sql, fields2));
			page.caculate();

			//取得FORM中传入的查询参数
			Object[] fields = new Object[3];
			
			fields[0] = theForm.getQryGroupID();
			fields[1] = page.getPos()-1;
			fields[2] = page.getPageSize();
			
			sql = servSql.getRecbyPageSql();

			list = bSqlServ.dbRead(cn, sql, fields, new DbGPictureService());
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}
	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List adlist(GPictureForm theForm,  ReturnMessage msg) 
	{
		List list = new Vector();

		Connection cn = null;
		Statement stmt=null;
		ResultSet rs=null;
		try
		{
			cn = DbcpPool.getConnection();
			
			GPictureServiceSqlMysql servSql = new GPictureServiceSqlMysql();
			
			StringBuffer sb = new StringBuffer(" where 1=1 ");
			
			if(theForm.getQryGAlbumID() > 0){
				
				sb.append(" and GalbumID = ");
				sb.append(theForm.getQryGAlbumID());
				sb.append(" ");
			}
			
			String  strOrderby = theForm.getQryOrderby().replaceAll(GlbFinal.SQLINjection,  " ");
			if(!strOrderby.equals("")){
				
				sb.append(" Order by ");
				sb.append(strOrderby);
				sb.append(" ");
			}else{				
				sb.append(" Order by Mtime desc ");
			}

			//查询记录数
			stmt = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			String sql = servSql.getCountSql3();

			logger.debug(sql+sb.toString());
			rs=stmt.executeQuery(sql+sb.toString());
			int nRows = 0;
			if (rs.next())
	           nRows = rs.getInt("COUNT");
			else
			{
	            rs.close();
	            stmt.close();
	            DbcpPool.returnConnection(cn);
			    return list;
			}
			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(nRows);
			page.caculate();
			
			if(theForm.getIsradnom().equals(GlbFinal.YES)){

				int pos = (int)(Math.random()*(page.getTotalNum()-20));
				page.setPos(pos);
			}			

			sb.append(" limit ");
			sb.append(page.getPos()-1);
			sb.append(", ");
			sb.append(page.getPageSize());
			
			 //取记录
			 sql = servSql.getRecSql();
			 logger.debug(sql+sb.toString());
			 DbGPictureService dbservice = new DbGPictureService();
			 
			 rs=stmt.executeQuery(sql+sb.toString());
			 while (rs.next())
			 {
				try
				{
					Object obj = dbservice.readSqlResultRecord(rs);
					if (obj != null) list.add(obj);
				}
				catch (Throwable t)
				{
					logger.error(t.getMessage());
				}		
			 }
			 rs.close();
			 stmt.close();
		     DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try {
				if (rs != null)
					rs.close();
				if (stmt != null)
					stmt.close();
				DbcpPool.returnConnection(cn);
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}			
	    }finally {
			try {
				if (rs != null)
					rs.close();
				if (stmt != null)
					stmt.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}			
			DbcpPool.returnConnection(cn);
		}		
		return list;
	}	

	/**
	 * 返回根据会员ID查询的会员操作日志
	 */
	public List list(String  userID) 
	{
		List list = null;
		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = userID;			

			GPictureServiceSqlMysql servSql = new GPictureServiceSqlMysql();
						
			BasicSqlService  bSqlServ = new BasicSqlService();

			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getByUserIDSql();
									
			list = bSqlServ.dbRead(cn, sql, fields, new DbGPictureService());
									
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return list;
	}
	/**
	 * 返回根据会员ID查询的会员操作日志
	 */
	public List newlist(String  userID) 
	{
		List list = null;
		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = userID;			

			GPictureServiceSqlMysql servSql = new GPictureServiceSqlMysql();
						
			BasicSqlService  bSqlServ = new BasicSqlService();

			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getByJoinUserIDSql();
									
			list = bSqlServ.dbRead(cn, sql, fields, new DbGPictureService());
									
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return list;
	}	
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public List views(GPictureForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		List list = new Vector();
		try
		{
			cn = DbcpPool.getConnection();

			GPictureServiceSqlMysql servSql = new GPictureServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			BaseGPicture prevbase  = null;
			BaseGPicture base  = null;
			BaseGPicture nextbase  = null;
		
			//取得前一张照片
			Object[] fields = new Object[2];
			
			fields[0] = theForm.getQryGpicID();			
			fields[1] = theForm.getQryGAlbumID();			

			String sql = servSql.getByPrevSql();			
					
			List tmplist = bSqlServ.dbRead(cn, sql, fields, new DbGPictureService());
			if(tmplist.size() > 0){
				prevbase = (BaseGPicture)tmplist.get(0);
			}
			
			//取得当前照片和下一张照片
			sql = servSql.getByNextSql();			
			
			tmplist = bSqlServ.dbRead(cn, sql, fields, new DbGPictureService());
			if(tmplist.size() == 0){
				common.setAppFailMsg(msg , " 取照片信息失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return list;				
			}
			base = (BaseGPicture)tmplist.get(0);
			BeanUtils.copyProperties(theForm, base);
			
			//保存下一张照片
			if(tmplist.size() > 1){
				nextbase = (BaseGPicture)tmplist.get(1);
			}

			if(prevbase != null ){
				
				list.add(prevbase);
				theForm.setIsExPrev(GlbFinal.YES);
			}
			list.add(base);
			if(nextbase != null){
				
				list.add(nextbase);
				theForm.setIsExNext(GlbFinal.YES);
			}
			
			DbcpPool.returnConnection(cn);
			
			return list;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return list;
	}
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void get(GPictureForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			List list = null;

			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = theForm.getGpicID();			
			
			GPictureServiceSqlMysql servSql = new GPictureServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeySql();			
					
			list = bSqlServ.dbRead(cn, sql, fields, new DbGPictureService());
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			
			BaseGPicture base = (BaseGPicture)list.get(0);
				
			BeanUtils.copyProperties(theForm, base);
						
			DbcpPool.returnConnection(cn);
			
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * 会员添加照片 添加到表中
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void uadd(GPictureForm theForm, String path, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);

			GPictureServiceSqlMysql servSql = new GPictureServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getInsertSql();
			//
			Object[] fields = new Object[8];
			boolean  res = true;
			
			GAlbumServiceImpl albumservice = new GAlbumServiceImpl();
			GAlbumForm albumForm= new GAlbumForm();
			
			//创建新相册
			if(!theForm.getAlbumname().equals("")){
				
				albumForm.setGroupID(theForm.getGroupID());
				albumForm.setAlbumname(theForm.getAlbumname());
				albumForm.setAlbuminfo(theForm.getAlbuminfo());
				albumForm.setProtect(theForm.getProtect());
				albumForm.setUserID(theForm.getUserID());
				albumForm.setDelflg(GlbFinal.YES);
				albumForm.setNum(1);
				
				albumservice.add(cn, albumForm, path, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setGalbumID(albumForm.getGalbumID());
			}
			
			//判断文件夹是否存在 （文件夹按年月建立 例如：201003）不存在则建立文件夹						
			String foldername = GlbFinal.FOLDER_USERPIC + GlbFinal.FOLDER_GROUPPIC 
				+ "group" + theForm.getQryGroupID() + "\\";
	
			FilesServiceImpl fileservice = new FilesServiceImpl();
			FilesForm fileForm = new FilesForm();
			
			//取得上传文件1
			FormFile file 	= theForm.getUpFile0(); 
			if(file != null &&!file.getFileName().equals(""))
	        {
	        	
				theForm.setGpicID(SequenceMgr.generateSeq(GlbFinal.SEQ_INITDATE,GlbFinal.SEQ_GPICTUREID));			
				
				fileForm.Clear();
				fileForm.setFormFile(file);
				fileForm.setFolder(foldername);
				fileForm.setF_info(theForm.getDescr0());
				fileForm.setF_Zip(GlbFinal.ZIP_600);
				fileForm.setImgHeight(theForm.getImgHeight());
				fileForm.setImgWidth(theForm.getImgWidth());
				
				fileservice.add(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setPicName(fileForm.getF_ID());

				fields[0] = theForm.getGpicID();			
				fields[1] = theForm.getGalbumID();
				fields[2] = theForm.getPicName();
				fields[3] = theForm.getIsHead();
				fields[4] = theForm.getUpCnt();
				fields[5] = theForm.getDownCnt();
				fields[6] = theForm.getUserID();
				fields[7] = DatetimeUtil.getDatetime();	        	
	        	
				res = bSqlServ.dbWrite(cn, sql, fields);
				if(!res){
					common.setAppFailMsg(msg , " 插入记录失败，请检查！");
					DbcpPool.rollBackTran(cn);
					DbcpPool.closeTran(cn);
					DbcpPool.returnConnection(cn);
					return;
				}	
				//更新照片数量
				if(theForm.getAlbumname().equals("")){
					albumForm.Clear();
					albumForm.setGalbumID(theForm.getGalbumID());
					albumForm.setNum(1);
					
					albumservice.updnum(cn, albumForm, msg);
					if(msg.getErrCode() != 0){
						
						return;
					}
				}
	        }
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about operlog" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}	
	/**
	 * 添加到表中
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void add(GPictureForm theForm, String path, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);

			GPictureServiceSqlMysql servSql = new GPictureServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getInsertSql();
			//
			Object[] fields = new Object[8];
			boolean  res = true;
			
			GAlbumServiceImpl albumservice = new GAlbumServiceImpl();
			GAlbumForm albumForm= new GAlbumForm();
			
			//判断文件夹是否存在 （文件夹按年月建立 例如：201003）不存在则建立文件夹						
			String foldername = GlbFinal.FOLDER_USERPIC + GlbFinal.FOLDER_GROUPPIC 
				+ "group" + theForm.getQryGroupID() + "\\";
	
			FilesServiceImpl fileservice = new FilesServiceImpl();
			FilesForm fileForm = new FilesForm();
			
			
			int num = 0;
			//取得上传文件1
			FormFile file 	= theForm.getUpFile0(); 
			if(file != null &&!file.getFileName().equals(""))
	        {
	        	
				theForm.setGpicID(SequenceMgr.generateSeq(GlbFinal.SEQ_INITDATE,GlbFinal.SEQ_GPICTUREID));			
				
				fileForm.Clear();
				fileForm.setFormFile(file);
				fileForm.setFolder(foldername);
				fileForm.setF_info(theForm.getDescr0());
				fileForm.setF_Zip(GlbFinal.ZIP_200_200);
				
				fileservice.add(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setPicName(fileForm.getF_ID());

				fields[0] = theForm.getGpicID();			
				fields[1] = theForm.getGalbumID();
				fields[2] = theForm.getPicName();
				fields[3] = theForm.getIsHead();
				fields[4] = theForm.getUpCnt();
				fields[5] = theForm.getDownCnt();
				fields[6] = theForm.getUserID();
				fields[7] = DatetimeUtil.getDatetime();	        	
	        	
				res = bSqlServ.dbWrite(cn, sql, fields);
				if(!res){
					common.setAppFailMsg(msg , " 插入记录失败，请检查！");
					DbcpPool.rollBackTran(cn);
					DbcpPool.closeTran(cn);
					DbcpPool.returnConnection(cn);
					return;
				}	
				num++;
				DbcpPool.commitTran(cn);
	        }
			//取得上传文件2
			file 	= theForm.getUpFile1(); 
			if(file != null &&!file.getFileName().equals(""))
	        {
	        	
				theForm.setGpicID(SequenceMgr.generateSeq(GlbFinal.SEQ_INITDATE,GlbFinal.SEQ_GPICTUREID));			
				
				fileForm.Clear();
				fileForm.setFormFile(file);
				fileForm.setFolder(foldername);
				fileForm.setF_info(theForm.getDescr1());
				fileForm.setF_Zip(GlbFinal.ZIP_200_200);
				
				fileservice.add(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setPicName(fileForm.getF_ID());

				fields[0] = theForm.getGpicID();			
				fields[1] = theForm.getGalbumID();
				fields[2] = theForm.getPicName();
				fields[3] = theForm.getIsHead();
				fields[4] = theForm.getUpCnt();
				fields[5] = theForm.getDownCnt();
				fields[6] = theForm.getUserID();
				fields[7] = DatetimeUtil.getDatetime();	        	
	        	
				res = bSqlServ.dbWrite(cn, sql, fields);
				if(!res){
					common.setAppFailMsg(msg , " 插入记录失败，请检查！");
					DbcpPool.rollBackTran(cn);
					DbcpPool.closeTran(cn);
					DbcpPool.returnConnection(cn);
					return;
				}	
				//更新照片数量
				num++;
				DbcpPool.commitTran(cn);
	        }
			//取得上传文件3
			file 	= theForm.getUpFile2(); 
			if(file != null &&!file.getFileName().equals(""))
	        {
	        	
				theForm.setGpicID(SequenceMgr.generateSeq(GlbFinal.SEQ_INITDATE,GlbFinal.SEQ_GPICTUREID));			
				
				fileForm.Clear();
				fileForm.setFormFile(file);
				fileForm.setFolder(foldername);
				fileForm.setF_info(theForm.getDescr2());
				fileForm.setF_Zip(GlbFinal.ZIP_200_200);
				
				fileservice.add(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setPicName(fileForm.getF_ID());

				fields[0] = theForm.getGpicID();			
				fields[1] = theForm.getGalbumID();
				fields[2] = theForm.getPicName();
				fields[3] = theForm.getIsHead();
				fields[4] = theForm.getUpCnt();
				fields[5] = theForm.getDownCnt();
				fields[6] = theForm.getUserID();
				fields[7] = DatetimeUtil.getDatetime();	        	
	        	
				res = bSqlServ.dbWrite(cn, sql, fields);
				if(!res){
					common.setAppFailMsg(msg , " 插入记录失败，请检查！");
					DbcpPool.rollBackTran(cn);
					DbcpPool.closeTran(cn);
					DbcpPool.returnConnection(cn);
					return;
				}	
				//更新照片数量
				num++;
				DbcpPool.commitTran(cn);
	        }
			//取得上传文件4
			file 	= theForm.getUpFile3(); 
			if(file != null &&!file.getFileName().equals(""))
	        {
	        	
				theForm.setGpicID(SequenceMgr.generateSeq(GlbFinal.SEQ_INITDATE,GlbFinal.SEQ_GPICTUREID));			
				
				fileForm.Clear();
				fileForm.setFormFile(file);
				fileForm.setFolder(foldername);
				fileForm.setF_info(theForm.getDescr3());
				fileForm.setF_Zip(GlbFinal.ZIP_200_200);
				
				fileservice.add(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setPicName(fileForm.getF_ID());

				fields[0] = theForm.getGpicID();			
				fields[1] = theForm.getGalbumID();
				fields[2] = theForm.getPicName();
				fields[3] = theForm.getIsHead();
				fields[4] = theForm.getUpCnt();
				fields[5] = theForm.getDownCnt();
				fields[6] = theForm.getUserID();
				fields[7] = DatetimeUtil.getDatetime();	        	
	        	
				res = bSqlServ.dbWrite(cn, sql, fields);
				if(!res){
					common.setAppFailMsg(msg , " 插入记录失败，请检查！");
					DbcpPool.rollBackTran(cn);
					DbcpPool.closeTran(cn);
					DbcpPool.returnConnection(cn);
					return;
				}	
				//更新照片数量
				num++;
				DbcpPool.commitTran(cn);
	        }
			//取得上传文件5
			file 	= theForm.getUpFile4(); 
			if(file != null &&!file.getFileName().equals(""))
	        {
	        	
				theForm.setGpicID(SequenceMgr.generateSeq(GlbFinal.SEQ_INITDATE,GlbFinal.SEQ_GPICTUREID));			
				
				fileForm.Clear();
				fileForm.setFormFile(file);
				fileForm.setFolder(foldername);
				fileForm.setF_info(theForm.getDescr4());
				fileForm.setF_Zip(GlbFinal.ZIP_200_200);
				
				fileservice.add(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setPicName(fileForm.getF_ID());

				fields[0] = theForm.getGpicID();			
				fields[1] = theForm.getGalbumID();
				fields[2] = theForm.getPicName();
				fields[3] = theForm.getIsHead();
				fields[4] = theForm.getUpCnt();
				fields[5] = theForm.getDownCnt();
				fields[6] = theForm.getUserID();
				fields[7] = DatetimeUtil.getDatetime();	        	
	        	
				res = bSqlServ.dbWrite(cn, sql, fields);
				if(!res){
					common.setAppFailMsg(msg , " 插入记录失败，请检查！");
					DbcpPool.rollBackTran(cn);
					DbcpPool.closeTran(cn);
					DbcpPool.returnConnection(cn);
					return;
				}
				num++;
				DbcpPool.commitTran(cn);
	        }
			if(num > 0){
				
				//更新照片数量
				albumForm.Clear();
				albumForm.setGalbumID(theForm.getGalbumID());
				albumForm.setNum(num);
				
				albumservice.updnum(cn, albumForm, msg);
				if(msg.getErrCode() != 0){
					
					return;
				}
				//更新用户动态
				OperLogServiceImpl logservice = new OperLogServiceImpl();
				OperLogForm logForm = new OperLogForm();
				
				logForm.setUserID(theForm.getUserID());
				logForm.setType(GlbFinal.OPERTYPE_UPLOADPIC);				//上传照片
				
				String[] arr = new String[7];
				arr[0] = JstlFun.GetUserNick(theForm.getUserID());			//主操作者		
				arr[1] = "";												//主动者与被动者的关系副词
				arr[2] = "";												//被动接受者ID
				arr[3] = "";												//被动接受者姓名
				arr[4] = "上传了 "+ num +" 张";								//操作
				arr[5] = "照片";												//操作对象名称
				arr[6] = String.valueOf(theForm.getGalbumID());				//操作对象KEY，多个字段以'|'分隔

				logForm.setLogarr(arr);
				
				logservice.add(cn, logForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}	
			}

			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about operlog" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}


	/**
	 * 删除一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delete(GPictureForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);

			delete(cn, theForm, msg);
			if(msg.getErrCode() != 0){
				return;
			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		return ;
	}
	/**
	 * 删除一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delete(Connection cn, GPictureForm theForm, ReturnMessage msg) 
	{
		
		try
		{

			GPictureServiceSqlMysql servSql = new GPictureServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			fields[0] = theForm.getGpicID();

			//取得原照片信息
			String sql = servSql.getbykeySql();			
			
			List list = bSqlServ.dbRead(cn, sql, fields, new DbGPictureService());
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}			
			BaseGPicture base = (BaseGPicture)list.get(0);
			BeanUtils.copyProperties(theForm, base);
			
			//删除物理文件信息
			FilesServiceImpl fileservice = new FilesServiceImpl();
			FilesForm fileform = new FilesForm();
			
			fileform.setF_ID(base.getPicName());
			
			fileservice.delete(cn, fileform, msg);
			if(msg.getErrCode() != 0){
				return;
			}
			
			//删除照片信息
			sql = servSql.getDeleteSql();
			
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 更新记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			
			GAlbumServiceImpl albumservice = new GAlbumServiceImpl();
			GAlbumForm albumForm = new GAlbumForm();
			
			//更新照片数量
			albumForm.Clear();
			albumForm.setGalbumID(base.getGalbumID());
			albumForm.setNum(-1);
			
			albumservice.updnum(cn, albumForm, msg);
			if(msg.getErrCode() != 0){
				
				return;
			}
			
			//删除该照片的所有点评信息
			GdianpingServiceImpl dpservice = new GdianpingServiceImpl();
			GdianpingForm dpform = new GdianpingForm();
			
			dpform.setDsort(GlbFinal.DSORT_PIC);
			dpform.setDsortID(theForm.getGpicID());
			
			dpservice.delbydsortID(cn, dpform, msg);
			if(msg.getErrCode() != 0){
				return;
			}
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
		}
		return ;
	}
	/**
	 * 更新一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void updUpdown(GPictureForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);

			updUpdown(cn, theForm, msg);
			if(msg.getErrCode() != 0){
				return;
			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		return ;
	}
	/**
	 * 更新一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void updUpdown(Connection cn, GPictureForm theForm, ReturnMessage msg) 
	{
		
		try
		{
			GPictureServiceSqlMysql servSql = new GPictureServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			fields[0] = theForm.getGpicID();

			//取得原照片信息
			String sql = servSql.getbykeySql();			
			
			List list = bSqlServ.dbRead(cn, sql, fields, new DbGPictureService());
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}			
			BaseGPicture base = (BaseGPicture)list.get(0);

			Object[] fields3 = new Object[3];
			
			fields3[0] = base.getUpCnt() + theForm.getUpCnt();
			fields3[1] = base.getDownCnt() + theForm.getDownCnt();
			fields3[2] = theForm.getGpicID();

			//
			sql = servSql.getUpUpdownSql();
			
			boolean  res = bSqlServ.dbWrite(cn, sql, fields3);
			if(!res){
				common.setAppFailMsg(msg , " 更新记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
		}
		return ;
	}
}
